package com.superpai.view.db;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

import com.superpai.common.Constants;
import com.superpai.domain.DBProduct;

/**
 * @author Devin.Hu
 * @date 2011-8-24
 * @version V1.0
 * @description 购物车数据库辅助类
 */
public class CartDBHelper extends BaseDBHelper {

	public CartDBHelper(Context context) {
		super(context, Constants.DBNAME, null, Constants.VERSION);
	}

	public CartDBHelper(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}
	
	/**
	 * 得到购物袋数量
	 * @return
	 */
	public long getCount(){
		long num = 0;
		SQLiteDatabase db = getReadableDatabase();
		Cursor cs = db.rawQuery("select count(*) from mycart ", null);
		cs.moveToFirst();
		num = cs.getLong(0);
		cs.close();
		db.close();
		return num;
	}
	
	/**
	 * 保存商品
	 * @param p
	 */
	public boolean saveItem(DBProduct p) {
		boolean flag = true;
		SQLiteDatabase db = getReadableDatabase();
		Cursor cs = db.rawQuery("select count(*) from mycart where pcode = ? ", new String[]{p.getPcode()});
		cs.moveToFirst();
		if (cs.getLong(0) > 0 ) {
			flag = false;
		}
		cs.close();
		db.close();
		if(flag){
			db = getWritableDatabase();
			db.execSQL("insert into mycart (pcode , pname , price, mprice ,pnum , pimg) values (?, ? , ? , ?, ? , ?)", new String[] { p.getPcode(), p.getPname(), p.getPrice() ,p.getMprice(), p.getPnum(), p.getPimg() });
			db.close();
		}
		return flag;
	}
	
	/**
	 * 根据商品编码查询商品信息
	 * @param id
	 * @return
	 */
	public void delItemByPcode(String pcode) {
		SQLiteDatabase db = getReadableDatabase();
		db.execSQL("delete from mycart where pcode = ? ", new String[]{pcode});
		db.close();
	} 
	
	/**
	 * 根据商品编码查询商品信息
	 * @param id
	 * @return
	 */
	public DBProduct findItemByPcode(String pcode) {
		DBProduct item = new DBProduct();
		SQLiteDatabase db = getReadableDatabase();
		Cursor cs = db.rawQuery("select * from mycart where pcode = ? ", new String[]{pcode});
		if(cs.moveToFirst()) {
			item.setPcode(cs.getString(cs.getColumnIndex("pcode")));
			item.setPname(cs.getString(cs.getColumnIndex("pname")));
			item.setPrice(cs.getString(cs.getColumnIndex("price")));
			item.setMprice(cs.getString(cs.getColumnIndex("mprice")));
			item.setPnum(cs.getString(cs.getColumnIndex("pnum")));
			item.setPimg(cs.getString(cs.getColumnIndex("pimg")));
		}
		cs.close();
		db.close();
		return item;
	} 
	
	/**
	 * 查询商品列表信息
	 * @param id
	 * @return
	 */
	public List<DBProduct> findItemList() {
		DBProduct item = null;
		List<DBProduct> mList = new ArrayList<DBProduct>();
		SQLiteDatabase db = getReadableDatabase();
		Cursor cs = db.rawQuery("select * from mycart ", null);
		
		while(cs.moveToNext()) {
			item = new DBProduct();
			item.setPcode(cs.getString(cs.getColumnIndex("pcode")));
			item.setPname(cs.getString(cs.getColumnIndex("pname")));
			item.setPrice(cs.getString(cs.getColumnIndex("price")));
			item.setMprice(cs.getString(cs.getColumnIndex("mprice")));
			item.setPnum(cs.getString(cs.getColumnIndex("pnum")));
			item.setPimg(cs.getString(cs.getColumnIndex("pimg")));
			mList.add(item);
		}
		cs.close();
		db.close();
		return mList;
	} 
	
	/**
	 * 得到购物袋总数量
	 * @param list
	 * @return
	 */
	public int getCartItemNum(List<DBProduct> list){
		int count = 0;
		if(null != list){
			for(DBProduct p : list){
				count += Integer.parseInt(p.getPnum());
			}
		}
		return count;
	}
	
	/**
	 * 得到购物袋商品总市场价
	 * @param list
	 * @return
	 */
	public double getCartMPrice(List<DBProduct> list){
		double total = 0.0;
		if(null != list){
			for(DBProduct p : list){
				total += Integer.parseInt(p.getPnum())*Double.parseDouble(p.getMprice());
			}
		}
		return total;
	}
	
	/**
	 * 得到购物袋商品总价
	 * @param list
	 * @return
	 */
	public double getCartTotalPrice(List<DBProduct> list){
		double total = 0.0;
		if(null != list){
			for(DBProduct p : list){
				total += Integer.parseInt(p.getPnum())*Double.parseDouble(p.getPrice());
			}
		}
		return total;
	}
	
	/**
	 * 得到优惠价格 
	 * @param list
	 * @return
	 */
	public double getOfferPrice(List<DBProduct> list){
		double total = getCartTotalPrice(list);
		double market = getCartMPrice(list);
		if(market > total){
			return (market - total) * getCartItemNum(list);
		}
		return 0.0;
	}
}
